import java.util.Arrays;
import java.util.Random;


public class NumbersUtil {
	private static int MAX = 15;
	
	/**
	 * 指定した要素数のint配列をランダムに生成する
	 * @param size 要素数
	 * @return
	 */
	public static int[] createNumbers(int size){
		Random rand = new Random();
		int[] ar = new int[size];
		int n = 0;
		for(int i = 0; i < size; i++){
			n = rand.nextInt(MAX);
			ar[i] = n;
		}
		Arrays.sort(ar);
		return ar;
	}

	/**
	 * 演習問題3-4の表示のヘッダ部分
	 * @param ar
	 */
	public static void displayHeader(int[] ar){
		StringBuilder line = new StringBuilder();
		line.append("   |");
		
		for( int i = 0; i < ar.length; i++ ){
			line.append(String.format("%4d", i));
		}
		line.append("\n");
		line.append("---+");
		for( int i = 0; i < ar.length; i++ ){
			line.append("----");
		}
		
		System.out.println(line);
		
	}

	public static void displaySearching(int[] ar, int low, int high, int mid){
		displayCursol(ar.length, low, high, mid);
		displayNumbers(ar, mid);
	}
	
	/**
	 * 探索範囲を表示する
	 * @param size
	 * @param low
	 * @param high
	 * @param mid
	 */
	public static void displayCursol(int size, int low, int high, int mid){
		StringBuilder line = new StringBuilder();
		line.append("   |");
		for( int i = 0; i < size; i++ ){
			if( i == low ){
				line.append(" <- ");
			}else if( i == mid ){
				line.append("   +");
			}else if( i == high ){
				line.append("  ->");
			}else{
				line.append("    ");
			}
		}
		System.out.println(line);
	}

	/**
	 * 配列の中身を表示する
	 * @param ar
	 */
	public static void displayNumbers(int[] ar, int mid){
		StringBuilder line = new StringBuilder();
		line.append(String.format("%3d", ar[mid]));
		line.append("|");
		for( int i = 0; i < ar.length; i++ ){
			line.append(String.format("%4d", ar[i]));
		}
		System.out.println(line);
	}

}
